﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CodeSHARPer.Model
{
    public class References
    {
        public References()
        {

        }

        /// <summary>
        /// Inits the instance and check for errors.
        /// </summary>
        /// <param name="name">The full path name or the full assembly name</param>
        public References(string name)
        {
            Assembly a = null;
            try
            {
                if (File.Exists(name))
                {
                    a = Assembly.LoadFile(name);
                }
                else
                {
                    // the assembly is not a path to the file, so we try to load the assembly and get its location
                    //the assembly must be in fullname, like System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
                    a = Assembly.Load(name);
                    this.IsIdentifiedByFullName = true;
                }
            }
            catch (Exception ex)
            {
                this.HasError = true;
                this.Error = ex.Message;
                this.Name = name;
            }

            //inits the properties
            if (a != null)
            {
                this.Name = a.FullName;
                this.Location = a.Location;
            }
        }

        /// <summary>
        /// The fullname of the assembly.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The fisical location of the assembly.
        /// </summary>
        public string Location { get; set; }

        /// <summary>
        /// Exception message found on load this assembly
        /// </summary>
        public string Error { get; set; }

        /// <summary>
        /// Flag indicating any exceptin found on load
        /// </summary>
        public bool HasError { get; set; }

        /// <summary>
        /// Flag indicating the asembly was loaded by its full name
        /// </summary>
        public bool IsIdentifiedByFullName { get; set; }

        /// <summary>
        /// Flag indicating this assembly was selected by user.
        /// </summary>
        public bool IsSeleted { get; set; }
    }
}
